/**
 * 
 */
package com.yst.metho.web.application.projectversion.actions.version;

import java.sql.SQLException;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.naming.NamingException;

import nextapp.echo2.app.list.ListSelectionModel;

import com.yst.metho.web.application.components.listingview.IConfirmationAction;
import com.yst.metho.web.application.projectversion.views.ProjectVersionTableModel;
import com.yst.metho.web.application.utils.Locator;
import com.yst.suivi.data.project.Project;
import com.yst.suivi.data.project.versions.ProjectVersion;
import com.yst.suivi.data.rights.Right;
import com.yst.suivi.status.StatusConstants;

/**
 * @author perso
 * 
 */
public class ClosePrjVersionAction implements IConfirmationAction
{
	private ProjectVersionTableModel mModel;

	private Project mProject;

	private ProjectVersion mSelectedVersion = null;

	/**
	 * @param model
	 */
	public ClosePrjVersionAction(ProjectVersionTableModel model, Project prj)
	{
		super();
		mModel = model;
		mProject = prj;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.IDeleteAction#doAction()
	 */
	public void doAction()
	{
		try
		{
			Locator.getProjectVersionView().closeProjectVersion(mSelectedVersion.getId());
		}
		catch (SQLException e)
		{
			throw new RuntimeException(e);
		}
		catch (EJBException e)
		{
			throw new RuntimeException(e);
		}
		catch (NamingException e)
		{
			throw new RuntimeException(e);
		}
		catch (FinderException e)
		{
			throw new RuntimeException(e);
		}
		catch (CreateException e)
		{
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.IDeleteAction#getConfirmationMessageParameter()
	 */
	public String getConfirmationMessageParameter()
	{
		return "la version " + mSelectedVersion.getVersion();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.IDeleteAction#setSelection(java.lang.Object)
	 */
	public void setSelection(Object source)
	{

		ListSelectionModel model = (ListSelectionModel) source;
		if (!model.isSelectionEmpty())
		{
			int selectedIndex = model.getMinSelectedIndex();
			mSelectedVersion = (ProjectVersion) mModel.getValueAt(0, selectedIndex);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.yst.metho.web.application.components.listingview.IDeleteAction#isEnabled()
	 */
	public boolean isEnabled()
	{
		return StatusConstants.CLOSE != mProject.getStatus() && StatusConstants.CLOSE != mSelectedVersion.getStatus();
	}

	public Right getRight()
	{
		return Right.CLOSE_PROJECT_VERSION;
	}

}
